package com.guotd.ticketSystemPlus.tools;

import com.guotd.ticketSystemPlus.entity.*;
import com.guotd.ticketSystemPlus.service.CabinService;
import com.guotd.ticketSystemPlus.service.FlightService;
import com.guotd.ticketSystemPlus.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.sql.Date;
import java.time.LocalDateTime;
import java.util.List;

@Component
@RequiredArgsConstructor
public class FlightTools {
    private final FlightService flightService;
    private final CabinService cabinService;
    private final OrderService orderService;
    @Tool(name = "queryFlights", description = "根据出发地、目的地和出发日期查询航班信息")
    public List<Flight> queryFlights(
            @ToolParam(description = "出发地") String fromAirport,
            @ToolParam(description = "目的地") String toAirport,
            @ToolParam(description = "出发日期，格式为：yyyy-MM-dd") String fromDate) {
        return flightService.searchFlights(fromAirport, toAirport, Date.valueOf(fromDate));
    }

    @Tool(name = "queryCabins", description = "航班的id查询该航班的客舱信息")
    public List<Cabin> queryCabins(@ToolParam(description = "航班id") String flightId)
    {
        return cabinService.getCabinsByFlightId(flightId);
    }

    @Tool(name = "createOrder", description = "预定机票")
    public void createOrder(
            @ToolParam(description = "用户id") String userId,
            @ToolParam(description = "航班id") String flightId,
            @ToolParam(description = "客舱id") String cabinId,
            @ToolParam(description = "乘客身份证号") String identity,
            @ToolParam(description = "乘客姓名") String name,
            @ToolParam(description = "乘客电话") String phone,
            @ToolParam(description = "乘客出生日期") Date birthday
    ){
        Flight flight = new Flight();
        Cabin cabin = new Cabin();
        User user = new User();
        cabin.setId(cabinId);
        flight.setId(flightId);
        user.setId(userId);
        Passenger passenger = new Passenger(identity, name, phone, birthday);
        Order order = new Order(null, user, passenger, flight, cabin, 1, LocalDateTime.now());
        orderService.addOrder(order);
    }
}
